<?php
/**
 * Retourne un Widget JQuery pour selectionner une date
 *
 * Options :
 *
 * - dateFormat : <ISO 8601 : 1979-04-07> Format de la date (Donnée transmise)
 * - userDateFormat : Format de la date affichée
 * - minDate: Date minimum
 * - maxDate: Date Maximum
 *
 * @author    Jacksay14
 * @package   widget
 * @since     2011
 */
class jckWidgetFormJQueryDateTimeField extends sfWidgetFormInputText
{
  protected function configure($options = array(), $attributes = array()) {
    parent::configure($options, $attributes);


    $this->addOption('dateFormat', 'yy-mm-dd');
    $this->addOption('timeFormat', 'hh:mm:ss');
    $this->addOption('separator', ' ');
    $this->addOption('formattedDateTime', 'DD d MM yy à hh h ii');
    $this->addOption('altTimeFormat', 'hh heures mm');
    $this->addOption('altTimeSeparator', ' à ');
    $this->addOption('minDate', 'null');
    $this->addOption('maxDate', 'null');
    
    
    $user_lang = explode('_', sfContext::getInstance()->getUser()->getCulture());
    $this->addOption('regional', $user_lang[0]);

  }



  public function render($name, $value = null, $attributes = array(), $errors = array())
  {

    $html = '
      {input}
      <script type="text/javascript">
      $($(document).ready(function(){
        
      	
        $("#{id}").datetimepicker({
          dateFormat: "{dateFormat}",
          regional: "{regional}",
          timeFormat: "{timeFormat}",
          onClose: function(dateText, inst) {
          	if(inst.id == "leave_period_start"){
		        var endDateTextBox = $("#leave_period_end");
		        if (endDateTextBox.val() != "") {
		            var testStartDate = new Date(dateText);
		            var testEndDate = new Date(endDateTextBox.val());
		            if (testStartDate > testEndDate)
		                endDateTextBox.val(dateText);
		        }
		        else {
		            endDateTextBox.val(dateText);
		        }
		    }
		    
		    if(inst.id == "leave_period_start"){
		    	var startDateTextBox = $("#leave_period_start");
		        if (startDateTextBox.val() != "") {
		            var testStartDate = new Date(startDateTextBox.val());
		            var testEndDate = new Date(dateText);
		            if (testStartDate > testEndDate)
		                startDateTextBox.val(dateText);
		        }
		        else {
		            startDateTextBox.val(dateText);
		        }
		    }
	    },
          onSelect: function(dateText, inst){
            if(inst.id == "leave_period_start"){
            	$("#leave_period_end").datetimepicker("setDate", (dateText));
            	
            	var start = $("#leave_period_start").datetimepicker("getDate");
        		$("#leave_period_end").datetimepicker("option", "minDate", new Date(start.getTime()) );
            }
			if(inst.id == "leave_period_end"){
				var end = $("#leave_period_end").datetimepicker("getDate");
        		$("#leave_period_start").datetimepicker("option", "maxDate", new Date(end.getTime()) );
			}
		  }
        });
        /****/
        dateTimePickerFriendly("{id}","{formattedDateTime}");
      }));
      </script>
    ';

    $template_vars = array(
        '{id}'    => $this->generateId($name),
        '{value}' => $value,
        '{dateFormat}' => $this->getOption('dateFormat'),
        '{regional}' => $this->getOption('regional'),
        '{minDate}' => $this->getOption('minDate'),
        '{maxDate}' => $this->getOption('maxDate'),
        '{formattedDateTime}' => $this->getOption('formattedDateTime'),
        '{timeFormat}' => $this->getOption('timeFormat'),
        /*'{separator}' => $this->getOption('separator'),*/
        '{altTimeSeparator}' => $this->getOption('altTimeSeparator'),
        '{input}' => parent::render($name, $value, $attributes, $errors)
    );

    return strtr($html, $template_vars);

  }




  /**
   *
   * @return <Array>
   */
  public function getJavaScripts() {
    return array_merge(
      array(
        sfConfig::get('app_jquery_js'),
        sfConfig::get('app_jquery_ui_js'),
        strtr(sfConfig::get('app_jquery_ui_datetime_i18n'), array('{culture}'=>$this->getOption('regional'))),
        sfConfig::get('app_jquery_ui_datetime_js'),
        '/jckJQueryWidgetPlugin/js/datepicker_friendly.js'
      ),
      parent::getJavaScripts()
    );
  }



  /**
   *
   * @return <Array>
   */
  public function  getStylesheets() {
    return array_merge(
      parent::getStylesheets(),
      array(
          sfConfig::get('app_jquery_ui_css') => 'screen'/*,
          sfConfig::get('app_jquery_ui_datetime_css') => 'screen'*/
      )
    );
  }
}
